//
//  DataBaseHandle2.m
//  UI_豆瓣项目
//
//  Created by dllo on 16/1/13.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DataBaseHandle2.h"
#import "Second1Model.h"
@implementation DataBaseHandle2

+ (DataBaseHandle2 *)sharedDataBaseHandle {
    // 静态修饰指针 （初始化一次）
    static DataBaseHandle2 *handle = nil;
    // 线程只执行一次
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handle = [[DataBaseHandle2 alloc] init];
    });
    return handle;
}

- (void)openDB {
    // 沙河路径
    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    // 拼接
    NSString *dbPath = [document stringByAppendingPathComponent:@"Movie.sqlite"];
    NSLog(@"%@", dbPath);
    
    // 打开数据库
    int result = sqlite3_open(dbPath.UTF8String, &dbPoint);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    } else {
        NSLog(@"打开失败");
    }
    
    
}
// 建表
- (void)createTable {
    
    NSString *sqlStr = [NSString stringWithFormat:@"create table if not exists Movie(number integer primary key autoincrement, name text)"];
    
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"建表成功");
    } else {
        NSLog(@"建表失败");
    }
    
    
}
// 增
- (void)insertModel:(Second1Model *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Model(name) values('%@')",model.name];
    int result = sqlite3_exec(dbPoint, sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    } else {
        NSLog(@"插入失败");
    }
}
// 删
- (void)removeModel:(Second1Model *)model {
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Model where name = '%@'", model.name];
    int result = sqlite3_exec(dbPoint,sqlStr.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    } else {
        NSLog(@"删除失败");
    }

}

- (NSMutableArray *)selectTable {
    // 查询数据的逻辑
    // 1. 从数据中读取数据
    // 2. 读取出来的数据给model赋值
    // 3. 返回装满model的数组
    
    // sql
    // * 通配符 把 * 改为字段名（name, sex）
    NSString *sqlStr = [NSString stringWithFormat:@"select * from Model"];
    
    // 创建一个跟随指针 用来遍历数据库中的每一条数据
    sqlite3_stmt *stmt = nil;
    
    // 进入查询
    // 第三个参数：字段的长度没有限制
    int result = sqlite3_prepare_v2(dbPoint, sqlStr.UTF8String, -1, &stmt, NULL);
    
    // 创建一个数组
    NSMutableArray *arrModel = [NSMutableArray array];
    if (result == SQLITE_OK) {
        // 遍历
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            // 从数据库中取出数据
            const unsigned char *name = sqlite3_column_text(stmt, 1);
            
            // 创建model 给model赋值
            Second1Model *model = [Second1Model secondModel];
            model.name = [NSString stringWithUTF8String:(const char *)name];
            
            
            [arrModel addObject:model];
            
            
        }
    }else {
        NSLog(@"查询失败");
    }
    return arrModel;
}




@end
